package com.topvision.myviewtest.gscview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.FrameLayout;

/**
 * @author lucy
 * @date 2019-01-21 11:56
 * @description //TODO
 */

public class GradientView extends FrameLayout {
    private int[] colors = new int[2];
    public static final int START = 105;
    public static final int END = 255;
    public static final int DIFF = 30;
    private Paint mPaint;

    public GradientView(Context context) {
        this(context, null);
    }

    public GradientView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GradientView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setWillNotDraw(false);
        mPaint = new Paint();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthSize = getSize(widthMeasureSpec);
        int heightSize = getSize(heightMeasureSpec);
        setMeasuredDimension(widthSize, heightSize);
    }

    private int getSize(int measureSpec) {
        int currentSize = 0;

        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);

        switch (mode) {
            //如果没有指定大小，就设置为默认大小
            case MeasureSpec.UNSPECIFIED: {
                currentSize = 300;
                break;
            }
            //如果测量模式是最大取值为size,我们将大小取最大值,你也可以取其他值
            case MeasureSpec.AT_MOST: {
                currentSize = size;
                break;
            }
            //如果是固定的大小，那就不要去改变它
            case MeasureSpec.EXACTLY: {
                currentSize = size;
                break;
            }
            default:
                break;
        }
        return currentSize;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setShader(updateLinearGradient());
        canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mPaint);
        postInvalidateDelayed(10);
    }

    /* 正向 */
    int color0;
    int color1;
    int color2;
    int color3;
    /* 反向 */
    int color4;
    int color5;
    int color6;
    int color7;
    /* 正向/方向 控制器 */
    boolean flag = true;

    public Shader updateLinearGradient() {
        LinearGradient linearGradient;
        if (flag) {
            color0++;
            if (color0 > DIFF) {
                color1++;
            }
            if (START + color0 > END) {
                color2++;
            }
            if (START + color1 > END) {
                color3++;
            }
            // blue->red(105,105,255)->(255,105,105)
            colors[0] = Color.rgb((START + color1) > END ? END : (START + color1), START, (END - color3) < START ? START : (END - color3));
            colors[1] = Color.rgb((START + color0) > END ? END : (START + color0), START, (END - color2) < START ? START : (END - color2));
            linearGradient = new LinearGradient(0, 0, getMeasuredWidth(), 30, colors, null, LinearGradient.TileMode.CLAMP);
            if (END - color3 < START) {
                flag = false;
                color0 = 0;
                color1 = 0;
                color2 = 0;
                color3 = 0;
            }
        } else {
            color4++;
            if (color4 > DIFF) {
                color5++;
            }
            if (START + color4 > END) {
                color6++;
            }
            if (START + color5 > END) {
                color7++;
            }
            // red->blue(255,105,105)->(105,105,255)
            colors[0] = Color.rgb(END - color7 < START ? START : (END - color7), START, (START + color5) > END ? END : (START + color5));
            colors[1] = Color.rgb(END - color6 < START ? START : (END - color6), START, (START + color4) > END ? END : (START + color4));
            linearGradient = new LinearGradient(0, 0, getMeasuredWidth(), 30, colors, null, LinearGradient.TileMode.CLAMP);
            if (END - color7 < START) {
                flag = true;
                color4 = 0;
                color5 = 0;
                color6 = 0;
                color7 = 0;
            }
        }
        return linearGradient;
    }
}
